package com.gilight.base.service.impl;

import com.gilight.base.dao.SysRoleMapper;
import com.gilight.base.dao.SysUserZzjgMapper;
import com.gilight.base.model.SysRole;
import com.gilight.base.model.SysUser;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.util.ShiroUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class SysUserZzjgServiceImpl implements SysUserZzjgService {
    @Resource
    private SysUserZzjgMapper sysUserZzjgMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<Map<String, Object>> getUserZzjgList(Map<String, Object> map) {
        List<Map<String, Object>> list = sysUserZzjgMapper.getUserZzjgList(map);
        for (Map<String, Object> userMap:list) {
            Long userId = MapUtils.getLong(userMap,"id");
            List<SysRole> roleList = sysRoleMapper.selectByUserId(userId);
            userMap.put("roleList",roleList);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getUserByZzjg(Map<String, Object> map) {
        List<Map<String, Object>> list = sysUserZzjgMapper.getUserByZzjg(map);
        for (Map<String, Object> userMap:list) {
            List<SysRole> roleList = sysRoleMapper.selectByUserId(MapUtils.getLong(userMap,"id"));
            userMap.put("roleList",roleList);
        }
        return list;
    }

    @Override
    public boolean isManager(String zzjgId) {
        int s = sysUserZzjgMapper.isManager(zzjgId);
        return s > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addManager(String zzjgId, String id) {
        sysUserZzjgMapper.cancelCurrentManager(zzjgId);//清除该部门的负责人
        sysUserZzjgMapper.setManager(zzjgId, id);//设置部门负责人
    }

    @Override
    public List<Map<String, Object>> getCurrentUserZzjgs() {
        SysUser sysUser = ShiroUtil.getUser();
        Long userId = sysUser.getId();

        return sysUserZzjgMapper.getUserZzjgsByUserId(userId);
    }
}
